<template>
	<view class="his-task-content">
		<view class="select-title mt10">
			<view class="flex-bc">
				<text class="title">起止时间</text>
				<text class="ml15 fs26 flex-vc">
					<text @click="changeTime('begin')">{{ form.begin }}</text>
					<text class="ml10 mr10">~</text>
					<text @click="changeTime('end')">{{ form.end }}</text>
					<i class="iconfont icon-calendar ml20"> </i>
				</text>
			</view>
			<view class="line"></view>
			<view class="flex-bc">
				<text class="title">检查类型</text>
				<listSelect v-model="typeCode" :list="typeList" @confirm="search" style="width: 385rpx"></listSelect>
			</view>
			<view class="line"></view>
			<view class="flex-bc">
				<text class="title">检查地区</text>
				<view class="flex-vc">
					<listSelect
						v-model="areaCode"
						:list="cityList"
						:defaultSelect="false"
						@confirm="confAreaCode"
						style="width: 185rpx"
					></listSelect>
					<listSelect
						:defaultSelect="false"
						v-model="areaCode2"
						:list="townList"
						@confirm="search"
						style="width: 185rpx"
					></listSelect>
				</view>
			</view>
		</view>

		<view class="message-sv mt10">
			<template v-if="dataList.length > 0">
				<view v-for="(item, index) in dataList" :key="index" class="table-box" @click="turnTo(item)">
					<view class="flex-vc">
						<i class="iconfont icon-a-zhanghuanquan1" style="color: #0085ff; font-size: 36rpx"></i>
						<text class="ml10 fs26 fw600">{{ item.areaName }}</text>
					</view>
					<view>
						<text style="color: #0085ff" class="fs24 fw600">{{ item.createTime }}</text>
					</view>
				</view>
			</template>
			<u-empty text="暂无数据" mode="list" v-else></u-empty>
		</view>

		<u-picker
			mode="time"
			:default-time="showTimeValue"
			v-model="timeShow"
			:params="timeParams"
			@confirm="confirmTime"
		></u-picker>
	</view>
</template>
<script>
	import listSelect from '@/components/common/select/listSelect.vue'
	import areaSelect from '@/components/common/select/areaSelect.vue'
	import sjsbApi from '@/api/yjzh/sjsb.js'
	import eventTool from '@/utils/event'
	import { debounce } from '@/utils/util'
	import moment from 'moment'
	export default {
		name: '',
		components: { listSelect, areaSelect },
		data() {
			return {
				form: {
					begin: moment().add(-24, 'hours').format('YYYY-MM-DD'),
					end: moment().format('YYYY-MM-DD')
				},
				showTimeValue: '',
				timeShow: false,
				timeParams: {
					year: true,
					month: true,
					day: true,
					hour: false,
					minute: false,
					second: false
				},
				areaCode: '',
				areaCode2: '',
				typeCode: 1,
				typeList: [
					{ label: '县级山洪灾害隐患排查表', value: 1 },
					{ label: '在建水利工程安全度汛检查表', value: 2 }
				],
				cityList: [],
				townList: [],

				dataList: []
			}
		},
		computed: {},
		async mounted() {
			this.getUserInfoData = uni.getStorageSync('getUserInfoData')
			this.getDeptTreeData = uni.getStorageSync('getDeptTreeData')
			console.info(`this.getUserInfoData.dept:`, this.getUserInfoData.dept)
			this.areaCode = this.getUserInfoData.dept.parentCode ? this.getUserInfoData.dept.parentCode : ''
			this.areaCode2 = this.getUserInfoData.dept.code

			let cityList = this.getDeptTreeData[0].children.map((item) => {
				return {
					label: item.name,
					value: item.code,
					children: item.children
				}
			})

			cityList.unshift({
				label: this.getDeptTreeData[0].name,
				value: this.getDeptTreeData[0].code,
				children: this.getDeptTreeData[0].children
			})
			this.cityList = cityList
			let index = this.cityList.findIndex((el) => el.value == this.areaCode)
			console.info(`index:`, index)
			if (index == -1) {
				index = 0
			}

			this.confAreaCode(null, this.cityList[index], false)
			console.info(`areaCode:`, this.areaCode)
			// this.search()
			// this.initPage()
		},
		methods: {
			changeTime(value) {
				this.timeTpye = value
				if (value == 'begin') {
					this.showTimeValue = this.form.begin
				} else {
					this.showTimeValue = this.form.end
				}
				this.timeShow = true
			},
			confirmTime(value) {
				let time = `${value.year}-${value.month}-${value.day}`
				let timtM = moment(time)
				if (this.timeTpye == 'begin') {
					if (timtM.isAfter(moment(this.form.end))) {
						uni.showToast({
							icon: 'error',
							title: '起始时间小于结束时间',
							duration: 1500
						})
						return
					} else {
						this.form.begin = time
					}
				} else {
					if (timtM.isBefore(moment(this.form.begin))) {
						uni.showToast({
							icon: 'error',
							title: '结束时间大于起始时间',
							duration: 1500
						})
						return
					} else {
						this.form.end = time
					}
				}
				this.timeShow = false
				this.search()
				console.info(`value:`, time)
			},
			confAreaCode(value, item, isAdd = true) {
				console.info(`item:`, item, value)
				this.townList = item.children.map((el) => {
					return {
						label: el.name,
						value: el.code
					}
				})
				this.townList.unshift({
					label: '全部',
					value: 'all'
				})
				if (isAdd) {
					this.areaCode2 = this.townList[0].value
				}
				this.search()
			},
			search: debounce(function (params) {
				this.$nextTick(() => {
					this.init()
				})
			}, 200),
			async init() {
				let areaCodes = []
				let parentItem = []
				console.info(`this.areaCode2 :`, this.areaCode2)
				console.info(`this.areaCode :`, this.areaCode)
				if (this.areaCode2 == 'all') {
					parentItem = this.findItemById(this.getDeptTreeData[0], this.areaCode)
				} else {
					parentItem = this.findItemById(this.getDeptTreeData[0], this.areaCode2)
				}
				console.info(`parentItem:`, parentItem)
				areaCodes = this.getChildCode([parentItem])

				// let missionTypeName = this.typeCode == 1 ? '' : '/' + this.typeCode

				let resp = await sjsbApi.securitycheckPage({
					areaCodes: areaCodes.filter((item) => !item.includes('all')),
					// areaName: '',
					// current: 1,
					missionType: this.typeCode,
					searchBeginTime: moment(this.form.begin).format('YYYY-MM-DD HH:mm:ss'),
					searchEndTime: moment(this.form.end).hour(23).format('YYYY-MM-DD HH:mm:ss')
					// size: 999
				})
				if (resp.code == 200) {
					this.dataList = resp.data.records
				}
			},
			findItemById(item, code) {
				console.info(`item, code:`, item, code)
				if (item.code === code) {
					return item
				}
				console.info(`item.children:`, item)
				if (!item.children) {
					return null
				} else {
					for (let i = 0; i < item.children.length; i++) {
						const found = this.findItemById(item.children[i], code)
						if (found) {
							return found
						}
					}
				}

				return null
			},
			getChildCode(arr) {
				let codes = []
				arr.forEach((el) => {
					codes.push(el.code)
					if (el.children && el.children.length > 0) {
						let res = this.getChildCode(el.children)
						codes.push(...res)
					}
				})

				return codes
			},
			turnTo(item) {
				uni.navigateTo({
					url: '/pages-subPackages/security/previewTable?data=' + encodeURIComponent(JSON.stringify(item))
				})

				// uni.$emit('eventsMap', this.dataList)
			}
		}
	}
</script>
<style lang="less" scoped>
	.his-task-content {
		width: 100%;
		height: 100%;
		padding: 10rpx 24rpx;
		.select-title {
			width: 100%;
			background: #f5f5f5;
			border-radius: 4rpx;
			// height: 60rpx;
			// background: #e4f2ff;
			display: flex;
			align-items: center;
			flex-direction: column;
			justify-content: space-between;
			padding: 0 10rpx;
			.flex-bc {
				width: 100%;
				font-size: 26rpx;
				padding: 13rpx 24rpx;
			}
			.line {
				height: 2rpx;
				width: 100%;
				color: #607589;
				background: rgba(96, 117, 137, 0.53);
				border-radius: 10rpx;
				opacity: 0.25;
			}
		}

		.message-sv {
			width: 100%;
			height: calc(100% - 275rpx);
			font-size: 26rpx;
			overflow: auto;

			.table-box {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 10rpx;
				padding: 0 10rpx;
				width: 700rpx;
				height: 80rpx;
				background: #edf5ff;
				border-radius: 4rpx;
			}
		}
	}
</style>
